/*
* Returns an new XMLHttpRequest object, or false if the browser
* doesn't support it
*/
var availableSelectList;
var countRow = 0;
var countCol = 0;

function newXMLHttpRequest() {
	
	var xmlreq = false;
	// Create XMLHttpRequest object in non-Microsoft browsers
	if (window.XMLHttpRequest) {
		xmlreq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
				// Try to create XMLHttpRequest in later versions
				// of Internet Explorer
				xmlreq = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e1) {
			// Failed to create required ActiveXObject
			try {
				// Try version supported by older versions
				// of Internet Explorer
				xmlreq = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e2) {
				// Unable to create an XMLHttpRequest by any means
				xmlreq = false;
			}
		}
	}
	return xmlreq;
};

/*
* Returns a function that waits for the specified XMLHttpRequest
* to complete, then passes it XML response to the given handler function.
* req - The XMLHttpRequest whose state is changing
* responseXmlHandler - Function to pass the XML response to
*/
function getReadyStateHandler(req, responseXmlHandler) {
	// Return an anonymous function that listens to the XMLHttpRequest instance
	return function () {
		// If the request's status is 'complete'
		if (req.readyState == 4) {
			// Check that we received a successful response from the server
			if (req.status == 200) {
				
				// Pass the XML payload of the response to the handler function.
				responseXmlHandler();
				$.unblockUI();
			} else {
				// An HTTP problem has occurred
				alert('HTTP error '+req.status+': '+req.statusText);
			}
		}
	};
};

function search(searchKey) {
	
	var keyValue = document.getElementById('getCities').value;
	keyValue = keyValue.replace(/^\s*|\s*$/g,'');
	if (keyValue.length > 0)
	{
	availableSelectList = document.getElementById('searchResult');
	var req = newXMLHttpRequest();
	req.onreadystatechange = getReadyStateHandler(req, update);
	req.open('POST','search.action', true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	var qString = $("#myForm").formSerialize();
	
	req.send(qString);
	}
};

function sendForm(action){
	
	var msg = '<img src="images/ajax-load.gif">';
	
	$.blockUI({ message: msg,
				css: { backgroundColor: '#DAE1EA'}
			});
	availableSelectList = document.getElementById('ResultSave');
	
	var req = newXMLHttpRequest();
	req.onreadystatechange = getReadyStateHandler(req, update);
	req.open('POST',action, true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	var qString = $("#myForm").formSerialize();
	req.send(qString);
};

function sendFormBP(action){
	
	var msg = '<img src="images/ajax-load.gif">';
	
	$.blockUI({ message: msg,
				css: { backgroundColor: '#DAE1EA'}
			});
	
	var req = newXMLHttpRequest();
	req.onreadystatechange = getReadyStateHandler(req, updateBP);
	req.open('POST',action, true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	var qString = $("#myFormBP").formSerialize();
	req.send(qString);
};

function updateBP()
{	
	for(var r = 1; r <= countRow; r++){
		for(var c = 1; c <= countCol; c++){
			document.getElementById('cell_Row'+r+'_Col'+c).className="";
			document.getElementById('cellCopy_Row'+r+'_Col'+c).value = document.getElementById('cell_Row'+r+'_Col'+c).value;
		}
	}
};

function update()
{
	var idx = 1;
	for(var i = 0; i < countRow; i++){
		
		document.getElementById('quantity_IDX_'+idx).className="";
		document.getElementById('quantity_Copy_'+idx).value = document.getElementById('quantity_IDX_'+idx).value; 
		document.getElementById('amountUnit_IDX_'+idx).className="";
		document.getElementById('amountUnit_Copy_'+idx).value = document.getElementById('amountUnit_IDX_'+idx).value;
		document.getElementById('uom_IDX_'+idx).className="";
		document.getElementById('uom_Copy_'+idx).value = document.getElementById('uom_IDX_'+idx).value;
		document.getElementById('payment_IDX_'+idx).className="";
		document.getElementById('payment_Copy_'+idx).value = document.getElementById('payment_IDX_'+idx).value;
		
		idx++;
	}
};

function changeValue(comp, currentV, oldV,rowcount, colcount){
	var currentValue = currentV.value;
	var oldValue = document.getElementById(oldV).value;
	var fs;
	if(currentValue != oldValue)
		document.getElementById(comp).className="changeValue";
	else
		document.getElementById(comp).className="";
//	for(var i=1; i<=rowcount; i++) {
//	   for(var j=1; j<=colcount; j++) {
	for(var i=rowcount; i>=1; i--) {
	   for(var j=colcount; j>=1; j--) {
		var formula=document.getElementById('cell_Row'+i+'_Col'+j).getAttribute('title');
		formula=formula.replace(' ','')
		if (formula!='null') {
		    var sum=0;
		    fs = formula.split(';');
		    var s=0;
		    for(var k=0; k<fs.length; k++) {
			var op='';
			if ((fs[k].charAt(0)=='R')||((fs[k].charAt(0)>'0')&&(fs[k].charAt(0)<='9'))) {
			  op='+';
			} else {
                          op=fs[k].charAt(0); 
			  fs[k]=fs[k].substr(1,fs[k].length);
			}

			fs2=fs[k].split(':');
			var i1=fs2[0].indexOf('C');
			var r1=parseInt(fs2[0].substr(1,i1-1)); 
			var c1=parseInt(fs2[0].substr(i1+1,fs2[0].length));
			var value=0;
			if (fs2.length==1) {
			if ((fs[k].charAt(0)>'0')&&(fs[k].charAt(0)<='9')) {//number
                             value=parseInt(fs2[0]);
			  } else {
                             value=parseInt(document.getElementById('cell_Row'+r1+'_Col'+c1).value);
			  }
			} else {	
			   var i2=fs2[1].indexOf('C');	
                           var r2=parseInt(fs2[1].substr(1,i2-1));
			   var c2=parseInt(fs2[1].substr(i2+1,fs2[1].length));
			   for(var x=r1;x<=r2;x++) {
			      for(var y=c1;y<=c2;y++)  {
 		  	  	value+=parseInt(document.getElementById('cell_Row'+x+'_Col'+y).value);
				 s++;
			      }	
			   }
			}
			switch (op) {
  			  case '+': sum+=value; break
  			  case '-': sum-=value; break
  			  case '*': sum*=value; break
  			  case '/': sum/=value; break
			}

		    }
		    document.getElementById('cell_Row'+i+'_Col'+j).value=sum;
		}


	   }		
	}
};

function hideunhide(elementID,interval) {
     var fs = interval.split(':');
     var i1=parseInt(fs[0]);
     var i2=parseInt(fs[1]); 

     if (document.getElementById(elementID).checked) {
        for(var k=i1; k<=i2; k++) {
	  $('#column'+k).hide();
	  $('td:nth-child('+(k+1)+')').hide();
        }
     } else {
        for(var k=i1; k<=i2; k++) {
	  $('#column'+k).show();
	  $('td:nth-child('+(k+1)+')').show();
	}
     }		

};
